package com.simpletour.qa.user.biz;

import com.simpletour.qa.common.base.BaseBiz;
import com.simpletour.qa.common.entity.Authority;
import com.simpletour.qa.common.entity.Role;
import com.simpletour.qa.user.repository.AuthorityJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @author zhaohw
 * @date 2018-06-26 15:00
 */
@Service
public class AuthorityBiz extends BaseBiz<AuthorityJpaRepository,Authority,Integer> {

    @Autowired
    private RoleBiz roleBiz;

    /**
     * 查询角色的所有权限信息
     * @param roleIds
     * @return
     */
    public Set<Authority> findAuthorityByRoleIds(String[] roleIds){
        Set<Authority> authorities = new HashSet<>();
        Stream.of(roleIds).forEach(id->{
            Optional<Role> roleOptional = roleBiz.findById(Integer.parseInt(id));
            roleOptional.ifPresent(role -> {
                authorities.addAll(role.getAuthorities());
            });
        });
        return authorities;
    }
}
